package com.shuwang.user.controller;

import com.shuwang.user.domain.LoadRequest;
import com.shuwang.user.domain.ShopSigninResponse;
import com.shuwang.user.domain.UserSigninResponse;
import com.shuwang.user.enums.SigninTypeEnums;
import com.shuwang.user.exception.BadRequestExitException;
import com.shuwang.user.exception.ForbiddenException;
import com.shuwang.user.exception.NotFoundException;
import com.shuwang.user.mapper.ShopInfoMapper;
import com.shuwang.user.mapper.UserSigninMapper;
import com.shuwang.user.model.ShopInfo;
import com.shuwang.user.model.ShopInfoExample;
import com.shuwang.user.model.UserSignin;
import com.shuwang.user.model.UserSigninExample;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Administrator on 2017/6/23 0023.
 */
@RestController
@RequestMapping(value = "userload")
@Api(value = "user_load",description = "用户登录API")
public class UserLoadController {

    protected final Logger log = LoggerFactory.getLogger(getClass());  //打日志用的

    @Autowired
    private UserSigninMapper userSigninMapper;

    @Autowired
    private ShopInfoMapper shopInfoMapper;


    @RequestMapping(value = "signinuser/load",method = RequestMethod.POST)
    @ApiOperation(value = "用户登录接口")
    public UserSigninResponse userLoadController(@ApiParam(value = "请输入用户登录实体类") @RequestBody LoadRequest request){

        UserSigninExample example = new UserSigninExample();
        example.createCriteria().andAccountEqualTo(request.getAccount());
        List<UserSignin> userSignins = userSigninMapper.selectByExample(example);

        if (userSignins.isEmpty()){
            log.debug("用户不存在");
            throw new NotFoundException("用户不存在");
        }

        if ( !request.getPassword().equals(userSignins.get(0).getPassword()) ){
            log.debug("密码错误");
            throw new BadRequestExitException("密码错误");
        }

        UserSigninResponse response = new UserSigninResponse();
        BeanUtils.copyProperties(userSignins.get(0),response);
        response.setAuthority(SigninTypeEnums.user.getName());
        return response;
    }

    @RequestMapping(value = "signinshop/load",method = RequestMethod.POST)
    @ApiOperation(value = "商铺登录接口")
    public ShopSigninResponse shopLoadController(@ApiParam(value = "请输入商铺登录实体类") @RequestBody LoadRequest request){

        UserSigninExample userSigninExample = new UserSigninExample();
        userSigninExample.createCriteria().andAccountEqualTo(request.getAccount());
        List<UserSignin> userSignins = userSigninMapper.selectByExample(userSigninExample);

        if (userSignins.isEmpty()){
            log.debug("用户不存在");
            throw new NotFoundException("用户不存在");
        }

        if ( !request.getPassword().equals(userSignins.get(0).getPassword()) ){
            log.debug("密码错误");
            throw new BadRequestExitException("密码错误");
        }

        if ( !userSignins.get(0).getAuthority().equals(SigninTypeEnums.shop.getValue())){
            log.debug("权限不足，无法登陆");
            throw new ForbiddenException("权限不足，无法登陆");
        }

        ShopInfoExample shopInfoExample = new ShopInfoExample();
        shopInfoExample.createCriteria().andUserIdEqualTo(userSignins.get(0).getUserId());
        List<ShopInfo> shopInfos = shopInfoMapper.selectByExample(shopInfoExample);

        ShopSigninResponse response = new ShopSigninResponse();
        BeanUtils.copyProperties(userSignins.get(0),response);
        BeanUtils.copyProperties(shopInfos.get(0),response);
        response.setAuthority(SigninTypeEnums.shop.getName());
        return response;
    }

    @RequestMapping(value = "signinadministrator/load",method = RequestMethod.POST)
    @ApiOperation(value = "管理员登录接口")
    public UserSigninResponse administratorLoadController(@ApiParam(value = "请输入管理员登录实体类") @RequestBody LoadRequest request){

        UserSigninExample example = new UserSigninExample();
        example.createCriteria().andAccountEqualTo(request.getAccount());
        List<UserSignin> userSignins = userSigninMapper.selectByExample(example);

        if (userSignins.isEmpty()){
            log.debug("用户不存在");
            throw new NotFoundException("用户不存在");
        }

        if ( !request.getPassword().equals(userSignins.get(0).getPassword()) ){
            log.debug("密码错误");
            throw new BadRequestExitException("密码错误");
        }

        if ( !userSignins.get(0).getAuthority().equals(SigninTypeEnums.administrator.getValue())){
            log.debug("权限不足，无法登陆");
            throw new ForbiddenException("权限不足，无法登陆");
        }

        UserSigninResponse response = new UserSigninResponse();
        BeanUtils.copyProperties(userSignins.get(0),response);
        response.setAuthority(SigninTypeEnums.administrator.getName());
        return response;
    }
}
